package rest

import (
	"strconv"

	"github.com/labstack/echo/v4"

	"bilinadmin/pkg/lueerror"

	pb "bilinadmin/api/pb/server"
	"bilinadmin/internal/service"
)

type bsHandler struct {
	*Base
	name string
}

// NewRestBrandHandler 品牌 handler
func NewRestBrandHandler() *bsHandler {
	return &bsHandler{
		Base: base,
		name: "rest_brand_handler",
	}
}

// Register 注册 handler
func (b *bsHandler) Register(r *echo.Group) {
	// 创建品牌
	r.POST("/v1/create", b.createBrand)
	// 更新品牌
	r.PUT("/v1/update", b.updateBrand)
	// 获取品牌信息
	r.GET("/v1/info", b.getBrand)
	// 创建门店
	r.POST("/v1/store/create", b.createStore)
	// 开通商城
	r.POST("/v1/mall/open", b.openMall)
	// 获取门店列表
	r.GET("/v1/stores", b.getStores)
	// 更新门店
	r.PUT("/v1/store/update", b.updateStore)
	// 获取店铺详情
	r.GET("/v1/stores/:store_id", b.getStoreInfo)
}

func (b *bsHandler) createBrand(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := b.VerifyAccountToken(c)
	if errL != nil {
		return errL
	}
	req := &pb.BrandCreateRequest{}
	if errL = b.ParseBody(c, req); errL != nil {
		return errL
	}
	ret, errL := service.GetBrandService().CreateBrand(ctx, req, token.GID, token.UserName, token.Mobile, token.Email)
	return b.SimpleRet(c, ret, errL)
}

func (b *bsHandler) updateBrand(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := b.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	req := &pb.BrandUpdateReq{}
	if errL = b.ParseBody(c, req); errL != nil {
		return errL
	}
	ret, errL := service.GetBrandService().UpdateBrand(ctx, token.BrandId, req)
	return b.SimpleRet(c, ret, errL)
}

func (b *bsHandler) getBrand(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := b.VerifyBilinToken(c)
	if errL != nil {
		return errL
	}
	ret, errL := service.GetBrandService().GetBrand(ctx, token.BrandId)
	return b.SimpleRet(c, ret, errL)
}

// 创建门店
func (b *bsHandler) createStore(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := b.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	req := &pb.StoreCreateReq{}
	if errL = b.ParseBody(c, req); errL != nil {
		return errL
	}
	if errL = b.MobileCheck(req.ContactMobile); errL != nil {
		return errL
	}
	ret, errL := service.GetBrandService().CreateStore(ctx, token.BrandId, token.EmpId, req)
	return b.SimpleRet(c, ret, errL)
}

// 开通星团
func (b *bsHandler) openMall(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := b.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	req := &pb.OpenMallReq{}
	if errL = b.ParseBody(c, req); errL != nil {
		return errL
	}
	errL = service.GetBrandService().OpenMall(ctx, token.BrandId, token.EmpId, req)
	return b.SimpleRet(c, nil, errL)
}

// 获取门店列表
func (b *bsHandler) getStores(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := b.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	status := c.QueryParam("status")
	busScope := c.QueryParam("business_scope")
	search := c.QueryParam("search")
	ret, errL := service.GetBrandService().SearchStoresList(ctx, token.BrandId, busScope, status, search)
	return b.SimpleRet(c, ret, errL)
}

// 更新门店信息
func (b *bsHandler) updateStore(c echo.Context) error {
	ctx := c.Request().Context()
	_, errL := b.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	req := &pb.StoreUpdateReq{}
	if errL = b.ParseBody(c, req); errL != nil {
		return errL
	}
	if req.ContactMobile != "" {
		if errL = b.MobileCheck(req.ContactMobile); errL != nil {
			return errL
		}
	}
	ret, errL := service.GetBrandService().UpdateStore(ctx, req)
	return b.SimpleRet(c, ret, errL)
}

func (b *bsHandler) getStoreInfo(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := b.VerifyBilinToken(c)
	if errL != nil {
		return errL
	}
	store := c.Param("store_id")
	storeId, err := strconv.ParseInt(store, 10, 64)
	if err != nil {
		return lueerror.InvalidRequest(err)
	}
	ret, errL := service.GetBrandService().GetStoreById(ctx, token.BrandId, storeId)
	return b.SimpleRet(c, ret, errL)
}
